package com.example.certificate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.certificate.dto.CustomerDTO;
import com.example.certificate.dto.UpdateCusDTO;
import com.example.certificate.entity.CustomerAddr;
import com.example.certificate.entity.User;
import com.example.certificate.execption.BusinessException;
import com.example.certificate.mapper.CustomerAddrMapper;
import com.example.certificate.service.CustomerAddrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hao
 * @since 2022-03-10 09:54:48
 */
@Service
public class CustomerAddrServiceImpl extends ServiceImpl<CustomerAddrMapper, CustomerAddr> implements CustomerAddrService {

    @Resource
    private CustomerAddrMapper customerAddrMapper;

    /**
     * &#x7ED9;&#x7528;&#x6237;&#x6DFB;&#x52A0;&#x6536;&#x8D27;&#x5730;&#x5740;
     *
     * @param dto
     * @param user
     */
    @Override
    public void addCustomer(CustomerDTO dto, User user) {
        //添加用户收货地址
        CustomerAddr customerAddr=new CustomerAddr();
        customerAddr.setArea(dto.getArea()); //区域
        customerAddr.setDetailedAddress(dto.getDetailedAddress()); //详细地址
        customerAddr.setName(dto.getName()); //姓名
        customerAddr.setUserId(user.getId()); //用户ID
        customerAddr.setPhone(dto.getPhone()); //手机号
        if(dto.getStatus()==1){
            CustomerAddr addr=customerAddrMapper.selectOne(new QueryWrapper<CustomerAddr>().eq("status",1)
                    .eq("user_id",user.getId()));
            if(addr!=null){
                addr.setStatus(0);
                this.updateById(addr); //修改之前的地址为不使用
            }
            customerAddr.setStatus(1); //设置为使用状态
        }else{
            customerAddr.setStatus(0); //设置为未使用状态
        }
        customerAddrMapper.insert(customerAddr); //添加
    }

    /**
     * 查询用户收货地址
     *
     * @param page
     * @return
     */
    @Override
    public Page<CustomerAddr> selCustomer(Page<CustomerAddr> page, User user) {
        page=customerAddrMapper.selectPage(page,new QueryWrapper<CustomerAddr>().eq("user_id",user.getId()));
        return page;
    }

    /**
     * 修改用户收货地址
     *
     * @param dto
     * @param user
     */
    @Override
    @Transactional(transactionManager = "transactionManager",propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateCus(UpdateCusDTO dto, User user) throws BusinessException {
        //修改用户之前选中的地址为未使用状态
        CustomerAddr customerAddr=customerAddrMapper.selectOne(new QueryWrapper<CustomerAddr>().eq("status",1).eq("user_id",user.getId()));

        customerAddr.setStatus(0);
        this.updateById(customerAddr); //修改之前选择的地址为未使用状态

        //查询用户新选择的收货地址
        CustomerAddr customer=customerAddrMapper.selectOne(new QueryWrapper<CustomerAddr>().eq("id",dto.getCustomerAddId())
                .eq("user_id",user.getId()));
        customer.setStatus(1);
        this.updateById(customer); //修改地址为使用状态
    }

    /**
     * 删除用户收货地址
     *
     * @param dto
     * @param user
     * @throws BusinessException
     */
    @Override
    public void delCus(UpdateCusDTO dto, User user){
        customerAddrMapper.deleteById(dto.getCustomerAddId());
        //删除用户收货地址
    }

    /**
     * 查询用户使用的地址
     *
     * @param user
     * @return
     */
    @Override
    public CustomerAddr selUserAddress(User user) {
        CustomerAddr customerAddr=customerAddrMapper.selectOne(new QueryWrapper<CustomerAddr>().eq("status",1));
        return customerAddr;
    }

}
